package com.information.manage.basic.controller;

import com.information.manage.basic.model.query.DepartmentAddQuery;
import com.information.manage.basic.model.query.DepartmentFindByConditionQuery;
import com.information.manage.basic.model.query.DepartmentMoveQuery;
import com.information.manage.basic.model.query.DepartmentUpdateQuery;
import com.information.manage.basic.model.vo.DepartmentListVO;
import com.information.manage.basic.model.vo.DepartmentTreeVO;
import com.information.manage.basic.common.result.Result;
import com.information.manage.basic.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sy
 * @since 2021-12-27
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    @PostMapping("/add")
    @ApiOperation(value = "新增部门", notes = "新增部门")
    public Result<Integer> addDepartment(@RequestBody DepartmentAddQuery departmentAddQuery){
        return departmentService.addDepartment(departmentAddQuery);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改部门", notes = "修改部门")
    public Result<Integer> updateDepartment(@RequestBody DepartmentUpdateQuery departmentUpdateQuery){
        return departmentService.updateDepartment(departmentUpdateQuery);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除部门", notes = "删除部门")
    public Result<Boolean> deleteDepartment(@RequestBody List<Integer> departmentIds){
        return departmentService.deleteDepartment(departmentIds);
    }

    @PutMapping("/move")
    @ApiOperation(value = "上下移", notes = "上下移")
    public Result<Boolean> moveDepartment(@RequestBody DepartmentMoveQuery departmentMoveQuery){
        return Result.ok(departmentService.moveDepartment(departmentMoveQuery.getId(), departmentMoveQuery.getUp()));
    }

    @GetMapping("/query")
    @ApiOperation(value = "id查询部门全路径", notes = "id查询部门全路径")
    public Result<String> queryDepartmentNameById(Integer id){
        return Result.ok(departmentService.queryDepartmentNameById(id));
    }

    @GetMapping("/download/excel")
    @ApiOperation(value = "下载模板", notes = "下载模板")
    public void downloadExcel(HttpServletResponse response) {
        departmentService.downloadExcel(response);
    }

    @GetMapping("/query/list/id")
    @ApiOperation(value = "查某个部门的子部门", notes = "查询某个部门的子部门")
    public Result<List<DepartmentListVO>> queryDepartmentListById(Integer id){
        return Result.ok(departmentService.queryDepartmentListById(id));
    }

    @GetMapping("/query/list/condition")
    @ApiOperation(value = "按条件查询部门", notes = "按条件查询部门")
    public Result<List<DepartmentTreeVO>> queryDepartmentByCondition(DepartmentFindByConditionQuery departmentFindByConditionQuery){
        return Result.ok(departmentService.queryDepartmentByCondition(departmentFindByConditionQuery));
    }

    @PostMapping("/excel/import")
    @ApiOperation(value = "部门导入", notes = "部门导入")
    public Result excelImport(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        return departmentService.excelImport(response, file);
    }

    @GetMapping("/excel/export")
    @ApiOperation(value = "部门导出", notes = "部门导出")
    public void exportExcel(HttpServletResponse response, Long typeCollection, String departmentName) {
        departmentService.exportExcel(response, typeCollection, departmentName);
    }

}
